﻿namespace QMERP.Infrastructure.Extensions
{
    public static class IntExtension
    {
        public static int ToInt(this object? obj)
        {
            if (obj == null)
                return 0;
            if (obj is Enum)
                return (int)obj;
            _ = int.TryParse(obj.ToString(), out int number);
            return number;
        }
        public static long ToLong(this object? obj)
        {
            if (obj == null)
                return 0;
            if (obj is Enum)
                return (long)obj;
            _ = long.TryParse(obj.ToString(), out long number);
            return number;
        }
        #region 数字转为Excel列名
        /// <summary>
        /// 数字转为Excel列名
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        public static string ToColumnChar(this int col)
        {
            //col从1开始
            string res = "";
            //col输入始终为非负数
            int remain = (col - 1) % 26;
            char addChar = (char)('A' + remain);
            res = addChar + res;
            col = (col - 1) / 26;
            while (col >= 1)
            {
                int left = (col - 1) % 26;
                char add = (char)('A' + left);
                res = add + res;
                col = (col - 1) / 26;
            }
            return res;
        }
        #endregion
    }
}
